﻿package app.view 
{
	import flash.net.URLRequest;
	import org.puremvc.as3.interfaces.IMediator;
    import org.puremvc.as3.patterns.mediator.Mediator;
	
    import app.model.DataProxy;
    import app.model.StageProxy;
	import app.view.event.UIEvent;
    import app.model.vo.Config;
    import app.ApplicationFacade;
    import org.puremvc.as3.interfaces.INotification;
	import app.view.vc.Img;
	
    import gs.*;
    import gs.easing.*;
    
	import flash.display.Sprite;
    import flash.events.Event;
	import flash.net.navigateToURL;
    
    /**
    * Author lzyy
    * Email:healdream@gmail.com
    * Blog:http://lab.lzyy.name/blog
    */
    public class ImgMediator extends Mediator implements IMediator
    {
        public static const NAME:String = 'ImgMediator';
        
		private var _link;//点击图片，链接
        public function ImgMediator(vc) 
        {
            super(NAME, vc);
            var sp:StageProxy = facade.retrieveProxy(StageProxy.NAME) as StageProxy;
            imgContainer.getStageInfo(sp.containerWidth, sp.containerHeight);
            imgContainer.addEventListener(Img.IMG_LOADED, _onImgLoaded);
            imgContainer.addEventListener(Img.NEXT_IMG, _onNextImg);
            imgContainer.addEventListener(Img.IMG_CLICKED, _onImgClicked);
            _loadImgs();
        }
        
        private function _loadImgs()
        {
            var dp:DataProxy = facade.retrieveProxy(DataProxy.NAME) as DataProxy;
            var data:Object = dp.getData();
            var imgurl:Array = data.imgurl;
			_link = data.link;
            imgContainer.x = Config.IMG_X;
            imgContainer.y = Config.IMG_Y;
            imgContainer.loadImgs(imgurl);
        }
        
        override public function listNotificationInterests():Array 
        {
            return [ApplicationFacade.NUMBTN_CLIKCED];            
        }

        override public function handleNotification( note:INotification ):void
        {
            switch(note.getName())
            {
                case ApplicationFacade.NUMBTN_CLIKCED:
                    var data = note.getBody();
                    _changeImg(data.index);
                    break;
            }
        }
        
        private function _changeImg(n = null,init:Boolean = false)
        {
			imgContainer.pauseAutoSwitch();
            var fadeIn:Sprite;
            var fadeOut:Sprite;
            if (init)
            {
                TweenLite.to(imgContainer.getChildByName('img' + n), 0.6, { alpha:1 } );
            }
            else if (n == null)
            {
                fadeIn = imgContainer.getChildByName('img' + imgContainer.currentImg);
                fadeOut = imgContainer.getChildByName('img' + imgContainer.prevImg);
                TweenLite.to(fadeIn, 0.6,  { alpha:1 } );
                TweenLite.to(fadeOut, 0.6,  { alpha:0 } );
                imgContainer.prevImg = imgContainer.currentImg;
            }
            else
            {
                imgContainer.currentImg = n;
                fadeIn = imgContainer.getChildByName('img' + n);
                fadeOut = imgContainer.getChildByName('img' + imgContainer.prevImg);
                TweenLite.to(fadeIn, 0.6,  { alpha:1 } );
                TweenLite.to(fadeOut, 0.6,  { alpha:0 } );
                imgContainer.prevImg = n;
            }
            sendNotification(ApplicationFacade.IMG_CHANGED, { index:imgContainer.currentImg } );
        }
        
        private function _onImgLoaded(evt:Event)
        {
            _changeImg(0, true);
            imgContainer.startAutoSwitch();
            
        }
        
        private function _onNextImg(evt:Event)
        {
            _changeImg();
        }
		
		private function _onImgClicked(evt:UIEvent)
		{
			var url = _link[evt.data.index];
			navigateToURL(new URLRequest(url), '_blank');
		}
        
        public function get imgContainer()
        {
            return viewComponent as Sprite;
        }
    }
    
}